package cn.com.compass.controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.compass.base.constant.BaseConstant;
import cn.com.compass.base.exception.BaseException;
import cn.com.compass.base.vo.BaseResponseVo;
import cn.com.compass.entity.Film;
import cn.com.compass.service.FilmService;
import cn.com.compass.util.DataXUtil;
import cn.com.compass.vo.AddFilmRequestVo;
import cn.com.compass.vo.FilmAppPageRequestVo;
import cn.com.compass.vo.FilmPcPageRequestVo;
import cn.com.compass.vo.UpdateFilmRequestVo;

@RestController
@RequestMapping("film")
@Validated
public class FilmController {
	
	@Autowired
	private FilmService service;
	/**
	 * 新增film
	 * @return
	 */
	@PostMapping
	public BaseResponseVo addFilm(@Valid @RequestBody AddFilmRequestVo vo) {
		try {
			Film f = new Film();
			DataXUtil.copyProperties(vo, f, vo.source2targetProperties());
			service.saveOne(f);
			return BaseResponseVo.success();
		} catch (Exception e) {
			throw new BaseException(BaseConstant.INNER_ERRO, e);
		}
	}
	
	/**
	 * 删除film
	 * @return
	 */
	@DeleteMapping("/{id}")
	public BaseResponseVo deleteFilm(@NotNull(message="电影主键Id不能为空") @PathVariable Long id) {
		service.deleteById(id);
		return BaseResponseVo.success();
	}
	
	/**
	 * 修改film
	 * @return
	 */
	@PutMapping("/{id}")
	public BaseResponseVo updateFilm(@NotNull(message="电影主键Id不能为空") @PathVariable Long id,@Valid @RequestBody UpdateFilmRequestVo vo) {
		try {
			Film f = service.findById(id);
			DataXUtil.copyProperties(vo, f, vo.source2targetProperties());
			service.updateOne(f);
			return BaseResponseVo.success();
		} catch (Exception e) {
			throw new BaseException(BaseConstant.INNER_ERRO, e);
		}
	}
	
	/**
	 * 查看电影明细
	 * @return
	 */
	@GetMapping("/{id}")
	public BaseResponseVo findFilm(@NotNull(message="电影主键Id不能为空") @PathVariable Long id) {
		return BaseResponseVo.success().setData(service.findById(id));
	}
	
	/**
	 * app端分页 dataId(主键id)+pageSize模式
	 * @return
	 */
	@PostMapping("/page/app")
	public BaseResponseVo findFilmAppPage(@Valid @RequestBody FilmAppPageRequestVo pageVo) {
		Specification<Film> spec = new Specification<Film>() {
			@Override
			public Predicate toPredicate(Root<Film> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return cb.like(root.get(Film.TITLE), "%"+pageVo.getTitle()+"%");
			}
		};
		return BaseResponseVo.success().setData(service.findAppPage(pageVo, spec));
	}
	
	/**
	 * pc端分页 pageNo+pageSize模式
	 * @return
	 */
	@PostMapping("/page/pc")
	public BaseResponseVo findFilmPcPage(@Valid @RequestBody FilmPcPageRequestVo pageVo) {
		Specification<Film> spec = new Specification<Film>() {
			@Override
			public Predicate toPredicate(Root<Film> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return cb.like(root.get(Film.TITLE), "%"+pageVo.getTitle()+"%");
			}
		};
		return BaseResponseVo.success().setData(service.findPcPage(pageVo, spec));
	}
	

}
